;
; nasm -g -f obj -l example1.lst example1.asm
; alink example1.obj -oPE -dll
; dlltool -z example1.def --export-all-symbol example1.dll
; dlltool -D example1.dll -d example1.def -l libexample1.a
; pexports example1.dll | sed "s/^_//" > example1.def
;

	global func1
		export func1
	
	global func2
		export func2
	
	global func3
		export func3
		
	global func4
		export func4
		
	global swap
		export swap
		
	global	init_matrix
		export	init_matrix
	
section data use32 class=data

section code use32 class=code

..start:

func1:
	push	ebp
	mov		ebp, esp
	
	mov		eax, 100	
	
	leave
	ret

func2:
	push	ebp
	mov		ebp, esp
	
	mov		eax, dword [ebp+08]		; eax=value#1
	mov		ecx, dword [ebp+12]		; ecx=value#2
	add		eax, ecx				; eax=eax+ecx
	xor		ecx, ecx				; ecx=0
	
	mov		esp, ebp
	pop		ebp
	ret

func3:
	push	ebp
	mov		ebp, esp

	mov		eax, dword [ebp+08]		; eax=value#1
	mov		ecx, dword [ebp+12]		; ecx=value#2
	mov		edx, dword [ebp+16]		; edx=value#3
	
	add		eax, ecx
	add		eax, edx
	
	mov		esp, ebp
	pop		ebp
	ret
	
func4:
	push	ebp
	mov		ebp, esp
	sub		esp, 16
	
	mov		ecx, dword [ebp+8]		; eax=value#1
	mov		edx, dword [ebp+12]		; edx=value#2
	
	mov		dword [ebp-4], 0		; int i=0 for outer loop #local variable
	mov		dword [ebp-8], 0		; int j=0 for inner loop #local varibale
	
.L1:
	cmp		dword[ebp-4], ecx
	je		.DONE
	mov		ebx, dword [ebp-4]
	shl		ebx, 2
	add		ebx, dword [ebp+12]		; address of matrix
	mov		ebx, dword [ebx]		; address of vectors start eax
	
	inc		dword [ebp-4]
	
	mov		dword [ebp-8], 0
.L2:
	cmp		dword[ebp-8], ecx
	je		.L1
	
	mov		eax, dword [ebp-4]
	dec		eax

	xor		edx, edx
	mov		edi, 2
	div		edi
	cmp		edx, 1
	je		.L3						; edx is even

	mov		eax, dword [ebp-4]
	dec		eax
	
	
	mul		ecx
	add		eax, dword[ebp-8]
	inc		eax
	mov		dword [ebx], eax
	
	jmp		.L4						; edx is odd
.L3:
	mov		eax, dword [ebp-4]
	mul		ecx
	sub		eax, dword[ebp-8]
	mov		dword [ebx], eax
.L4:
	inc		dword [ebp-8]
	add		ebx, 4
	jmp		.L2

	jmp		.L1
	
.DONE:
	xor		eax, eax
;	mov		eax, edx
	
	leave
	ret	
	
swap:
	push	ebp
	mov		ebp, esp
	
	xor		eax, eax
	
	leave
	ret
	
fill_vector:
	push	ebp
	mov		ebp, esp
	
	and		esp, -16
	sub		esp, 48
	
	mov		eax, dword [ebp+8]		; first argument. Vector element count N
	mov		edx, dword [ebp+12]		; vector pointer address
	
	mov		dword [ebp-4], 0		; local variable i=0
.L1:
	mov		eax, dword [ebp+8]		; get element count N
	cmp		eax, dword [ebp-4]		; compare with local loop variable i
	je		.DONE					; if (i==N) goto DONE
	
	sal		eax, 2
	add		eax, dword [edx]		; get vector element address with index i
	mov		dword [eax], 10
	
	inc		dword [ebp-4]			; increment i
	jmp		.L1						; continue loop
.DONE:

	xor		eax, eax
	
	leave
	ret
	
init_matrix:
	push	ebp
	mov		ebp, esp
	and		esp, -16
	sub		esp, 48
	
	mov		eax, dword [ebp+8]		; value#1 N
	mov		eax, dword [ebp+12]		; value#2 M
	mov		eax, dword [ebp+16]		; value#3 matrix
	
	mov		eax, dword [ebp+16]		; address of matrix pointter
	mov		eax, dword [eax]		; address of matrix object
	
	mov		dword[ebp-4], 0			; local variable i=0
	mov		dword[ebp-8], 0			; local variable j=0
.L1:
	mov		eax, dword[ebp+8]		; copy first argument of function. first dimension of matrix
	cmp		dword[ebp-4], eax		; comparing i with N
	je		.DONE
	
	inc		dword[ebp-4]			; incrimenting i++
	jmp		.L1	
	
.DONE:
	
	leave
	ret

